package UIComponent 
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import com.raoway.manager.StageProxy;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.utils.getDefinitionByName;
	import game.events.EventName;
	import game.events.GameDispatcher;
	import game.events.ParamEvent;
	import game.manager.ResCacheManager;
	import mx.utils.StringUtil;
	/**
	 * ...
	 * @author gcxk721
	 */
	public class ResLoading extends Sprite
	{
		//单例
		private static var instance : ResLoading;
		
		private var _total : int;
		private var _text : String;
		private var _progress : Number;
		
		private var _skin : MovieClip;
		private var _shape : Shape;
		private var _startWidth : Number;
		private var _childProgress : Number;
        private var _totalTime : int = 15;
        private var picLoader : Loader;
		
		public function ResLoading() 
		{
			if (instance != null)
			{
				throw new Error("single instance");
			}
		}
		
		private function configUI():void{
            this._skin = new (getDefinitionByName("ResLoadingSkin") as Class)();
            addChild(this._skin);
            this._startWidth = 535;
            this._skin.loadingMc.light.x = 0;
            this._skin.refreshBtn.addEventListener(MouseEvent.CLICK, this.clickRefreshHandler);
            StageProxy.stage.addEventListener(Event.RESIZE, this.resizeHandler);
            this.resizeHandler(null);
            this.update();
        }
		
		private function clickRefreshHandler(e:MouseEvent):void 
		{
			ResCacheManager.getInstance().clearCache();
            GameDispatcher.getInstance().dispatchEvent(new ParamEvent(EventName.SHOW_DEBUG));
		}
		
		private function resizeHandler(evt : Event):void
		{
            this._skin.x = int(StageProxy.rightOffset) >> 1;
            this._skin.y = int(StageProxy.downOffset) >> 1;
            this.drawBG();
        }
		
		private function drawBG():void
		{
            if (this._shape == null)
			{
                this._shape = new Shape();
            } 
			else 
			{
                this._shape.graphics.clear();
            }
            _shape.graphics.beginFill(0);
            this._shape.graphics.drawRect(-(StageProxy.leftOffset()), -(StageProxy.upOffset()), StageProxy.stageWidth(), StageProxy.stageHeight());
            addChildAt(this._shape, 0);
        }
		
		private function update():void
		{
            var totalPro : Number;
            var totalTxt : String;
            var childTxt : String = "";
            if (this._childProgress != 0)
			{
                childTxt = (int((this._childProgress * 100)) + "%");
            }
            if (this.total == 0)
			{
                totalTxt = "0%";
                this._skin.loadingMc.loadingTxt.text = ("总进度：" + totalTxt);
                this._skin.loadingMc.light.x = 0;
                this._skin.loadingMc.timeTxt.htmlText = StringUtil.substitute("剩余时间 {0} 秒", this._totalTime);
                this._skin.loadingMc2.loadingTxt.text = ("当前进度：" + totalTxt);
                this._skin.loadingMc2.light.x = 0;
                this._skin.loadingMc2.timeTxt.htmlText = "";
            } 
			else 
			{
                totalPro = Math.min(1, (this.progress / this.total));
                totalTxt = int((totalPro * 100)) + "%";
                this._skin.loadingMc.loadingTxt.text = ("总进度：" + totalTxt);
                this._skin.loadingMc.light.x = (totalPro * this._startWidth);
                this._skin.loadingMc.timeTxt.htmlText = StringUtil.substitute("剩余时间 {0} 秒", String(Math.ceil(((1 - totalPro) * this._totalTime))));
                this._skin.loadingMc2.loadingTxt.text = ("当前进度：" + childTxt);
                this._skin.loadingMc2.light.x = (this._childProgress * this._startWidth);
                this._skin.loadingMc2.timeTxt.htmlText = "";
            }
        }
		
		public function get total() : int
		{
			return _total;
		}
		
		public function reset() : void
		{
			_total = 0;
		}
		
		public function loadImg(url:String = ""):void
		{
            this.picLoader = new Loader();
            this.picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.completePicHandler);
            this.picLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorPicHandler);
            this.picLoader.load(new URLRequest(rURL(url)));
        }
		
		private function ioErrorPicHandler(evt : IOErrorEvent):void 
		{
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function completePicHandler(evt : Event):void 
		{
			this._skin.picContainer.addChild(LoaderInfo(evt.target).content);
            this.dispatchEvent(new Event(Event.COMPLETE));
		}
		
		public function set progress(value : Number):void
		{
            this._progress = value;
        }
		
		public function get progress():Number
		{
            return this._progress;
        }
		
        public function setProgress(value1 : Number, value2 : Number, value3 : Number = 0, totalTime : Number = 15):void
		{
            if (this._childProgress < 1)
			{
                this._childProgress = (this._childProgress + 0.02);
            } 
			else 
			{
                this._childProgress = 0.1;
            }
            if (this._childProgress > 1)
			{
                this._childProgress = 1;
            }
            if ((value1 / value2) < (this.progress / this.total))
			{
                return;
            }
            this._totalTime = totalTime;
            this.progress = this.progress - ((this.progress - (value1 / value2)) * 0.2);
            this._total = 1;
            if (this.progress == this.total)
			{
				
            }
            this.update();
        }
		
		public function close():void
		{
            this.dispatchEvent(new Event(Event.CLOSE));
            if (this.picLoader != null)
			{
                this.picLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.completePicHandler);
                this.picLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, this.ioErrorPicHandler);
                this.picLoader.unload();
				picLoader = null;
            }
        }
		
		public function setText(value : String):void 
		{
			
        }
		
		public static function getInstance() : ResLoading
		{
			if (instance == null)
			{
				instance = new ResLoading();
			}
			return instance;
		}
		
	}

}